﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using RevitStarter.Util;

namespace RevitStarter
{
    using System.Diagnostics;
    using Autodesk.RevitAddIns;

    static class Program
    {
        public const string IniFileName = "RevitStarter.ini";


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string iniPath = Path.Combine(Application.StartupPath, IniFileName); //配置文件路径
            if (File.Exists(iniPath))
            {
                var iconName = IniHelper.GetSetting("Config", "icon", iniPath);
                var titleName = IniHelper.GetSetting("Config", "title", iniPath);
                var pictureName = IniHelper.GetSetting("Config", "picture", iniPath);
                List<string> iniRevitList = IniHelper.GetSetting("Config", "revitversion", iniPath).Split(',').ToList();
                var addInName = IniHelper.GetSetting("Config", "addinname", iniPath);

                //#Todo 暂不进行参数有效性验证
                List<RevitProduct> revitVersion = AddinUtil.FilterValidRevitVersions(iniRevitList);
                if (revitVersion.Count == 0)
                {
                    string rvtLst = String.Empty;

                    for (int i = 0; i < iniRevitList.Count; i++)
                    {
                        string s = iniRevitList[i];
                        rvtLst += s;
                        if (i != iniRevitList.Count - 1)
                        {
                            rvtLst += ",";
                        }
                    }

                    UiUtil.ErrorMessageBox("在此电脑上未能查询到支持的Revit版本!\n本软件仅支持以下版本:" + rvtLst);
                    return;
                }

                //直接启动exe
                if (args.Length == 0)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new StarForm(revitVersion, iconName, titleName, pictureName, addInName));
                }
                else
                {
                    var str = args[0];
                    str = str.ToLower();


                    if (str.CompareTo("install") == 0) //安装
                    {
                        //2020-12-19 拷贝替换
                        if (false)
                        {
                            string srcReplacePath = Path.Combine(Application.StartupPath, "Replace"); //替换文件路径

                            DirectoryInfo dr1 = new DirectoryInfo(Application.StartupPath);
                            Debug.Assert(dr1 != null);
                            DirectoryInfo dr2 = dr1.Parent;
                            Debug.Assert(dr2 != null, nameof(dr2) + " != null");
                            string desReplacePath = dr2.FullName; //返回父亲路径
                            PathUtil.Copy(srcReplacePath, desReplacePath);
                        }

                        foreach (var rvtVer in revitVersion)
                        {
                            AddinUtil.UpdateAddInFile(rvtVer, addInName);
                        }
                    }
                    else if (str.CompareTo("uninstall") == 0) //卸载
                    {
                        foreach (var rvtVer in revitVersion)
                        {
                            AddinUtil.DeleteAddInFile(rvtVer, addInName);
                        }
                    }
                }
            }
            else
            {
                UiUtil.ErrorMessageBox("缺少以下配置文件!" + iniPath);
                return;
            }
        }
    }
}